
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    isScorll: false,
    pageSize: 10,
    pageNum: 0,
    show: false,
    name: '',
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  deletClick(e){
    console.log(e);
    let that = this
    db.collection('siteInfo').doc(e.target.dataset.id).remove({
      success: function(res) {
        that.setData({
          pageNum: 0,
          list: [],
        },()=>{
          that.getList()
        })
      }
    })
  },
  moreClick(e){
    console.log(e);
    const storeId = e.target.dataset.storeId; 
    for (const item of this.data.list) {
        if (item._id === storeId) continue;
        this.selectComponent("#vanSwipeCell"+item._id).close();
    } 
    wx.nextTick(()=>{this.selectComponent("#vanSwipeCell" + storeId).open({position: "right"})})
  },
  getList(){
    this.setData({isScorll: false})
    let that = this
    db.collection('siteInfo').orderBy('date', 'desc').limit(that.data.pageSize).skip(that.data.pageNum).get().then(res=>{
      console.log(res);
      let oldData = that.data.list
      let newData = oldData.concat(res.data)
      if(res.data.length != 0){
        that.setData({
          isScorll: true
        })
      }
      that.setData({
        list: newData
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({list: []},()=>{this.getList()})
  },
  addemployee(){
    this.setData({show: true, name: ''})
  },
  canel(){
    this.setData({show: false})
  },
  confirm(){
    console.log(this.data.name);
    if(!this.data.name){
      wx.showToast({ title: '请输入名称', icon: 'none' });
      return
    }
    this.setData({loading: true})
    let data = {
      date: this.timestampToTime(Date.now()),
      name: this.data.name
    }
    db.collection('siteInfo').add({data: data}).then(d=>{
      wx.showToast({ title: '成功', icon: 'none' });
      this.setData({
        show: false,
        loading: false,
        pageNum: 0,
        list: [],
      },()=>{
        this.getList()
      })
    })
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  bindscrolltolower(){
    if(this.data.isScorll){
      let pageNum = this.data.pageNum + this.data.pageSize
      this.setData({
        pageNum: pageNum,
        pageSize: this.data.pageSize
      }, ()=>{this.getList()})
    }
  },
  timestampToTime(timestamp) {
    timestamp = timestamp ? timestamp : null;
    let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})